package com.hopes.bizz.hazard.alarm.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.hazard.alarm.dto.AlarmAnalysisRequest;
import com.hopes.bizz.hazard.alarm.dto.AlarmStatisticsRequest;
import com.hopes.bizz.hazard.alarm.entity.HazAlarmBrokenEntity;
import com.hopes.bizz.hazard.alarm.entity.HazAlarmOverproofEntity;
import com.hopes.bizz.hazard.alarm.service.HazAlarmBrokenService;
import com.hopes.bizz.hazard.alarm.service.HazAlarmOverproofService;
import com.hopes.bizz.hazard.alarm.service.HazAlarmStatisticsService;
import com.hopes.bizz.hazard.alarm.vo.HazAlarmStatistics;
import com.hopes.bizz.hazard.common.constant.HazardDicConstant;
import com.hopes.bizz.hazard.om.dto.HazRtDataHisDTO;
import com.hopes.boss.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author daijie
 * @version 1.0
 * @date 2023/11/20 19:13
 * @Description:
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarmStatistics" )
@Tag(description = "alarmStatistics" , name = "报警信息统计" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HazAlarmStatisticsController {

    private final HazAlarmStatisticsService hazAlarmStatisticsService;
    private final HazAlarmBrokenService hazAlarmBrokenService;
    private final HazAlarmOverproofService hazAlarmOverproofService;

    /**
     * 断网报警数据统计
     * @return R
     */
    @Operation(summary = "断网报警数据统计" , description = "断网报警数据统计" )
    @GetMapping("broken/page" )
    public R getBrokenStatistics(Page page, AlarmStatisticsRequest request) {
        List<HazAlarmStatistics> statistics = hazAlarmStatisticsService.getBrokenStatistics(page, request);
        page.setRecords(statistics);
        return R.ok(page);
    }

    /**
     * 超标报警数据统计
     * @return R
     */
    @Operation(summary = "超标报警数据统计" , description = "超标报警数据统计" )
    @GetMapping("overproof/page" )
    public R getOverproofStatistics(Page page, AlarmStatisticsRequest request) {
        List<HazAlarmStatistics> statistics = hazAlarmStatisticsService.getOverproofStatistics(page, request);
        page.setRecords(statistics);
        return R.ok(page);
    }

    /**
     * 通过危险源id查询所有报警
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过危险源id查询所有报警", description = "通过危险源id查询所有报警")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable("id") Long id) {
        List<HazAlarmBrokenEntity> alarmBrokenList = hazAlarmBrokenService.list
                (Wrappers.lambdaQuery(HazAlarmBrokenEntity.class).eq(HazAlarmBrokenEntity::getHazardSourceId, id).eq(HazAlarmBrokenEntity::getAlarmStatus, HazardDicConstant.HAZ_ALARM_STATUS_ALARM));
        List<HazAlarmOverproofEntity> alarmOverproofList = hazAlarmOverproofService.list
                (Wrappers.lambdaQuery(HazAlarmOverproofEntity.class).eq(HazAlarmOverproofEntity::getHazardSourceId, id).eq(HazAlarmOverproofEntity::getAlarmStatus, HazardDicConstant.HAZ_ALARM_STATUS_ALARM));
        List<Object> result = new ArrayList<>();

        result.addAll(alarmBrokenList);
        result.addAll(alarmOverproofList);
        return R.ok(result);
    }

    /**
     * 查询超标报警统计分析
     * @return R
     */
    @Operation(summary = "查询超标报警统计分析" , description = "查询超标报警统计分析" )
    @GetMapping("analysisOverproof" )
    public R analysisOverproof(AlarmAnalysisRequest request) {
        if (CollectionUtils.isEmpty(request.getMonitorFactorIds())) {
            return R.failed("请选择危险源！");
        }
        return R.ok(hazAlarmStatisticsService.analysisOverproof(request));
    }

    /**
     * 查询断网报警统计分析
     * @return R
     */
    @Operation(summary = "查询断网报警统计分析" , description = "查询断网报警统计分析" )
    @GetMapping("analysisBroken" )
    public R analysisBroken(AlarmAnalysisRequest request) {
        if (CollectionUtils.isEmpty(request.getMonitorFactorIds())) {
            return R.failed("请选择危险源！");
        }
        return R.ok(hazAlarmStatisticsService.analysisBroken(request));
    }

    /**
     * 查询月报统计分析
     * @return R
     */
    @Operation(summary = "查询月报统计分析" , description = "查询月报统计分析" )
    @GetMapping("monitorMonth" )
    public R monitorMonth(AlarmAnalysisRequest request) {
        return R.ok(hazAlarmStatisticsService.monitorMonth(request));
    }

    /**
     * 查询年报统计分析
     * @return R
     */
    @Operation(summary = "查询年报统计分析" , description = "查询年报统计分析" )
    @GetMapping("monitorYear" )
    public R monitorYear(AlarmAnalysisRequest request) {
        return R.ok(hazAlarmStatisticsService.monitorYear(request));
    }

    /**
     * 查询危险源历史监控数据
     * @return R
     */
    @Operation(summary = "查询危险源历史监控数据" , description = "查询危险源历史监控数据" )
    @GetMapping("rtDataHis" )
    public R monitorYear(HazRtDataHisDTO hazRtDataHisDTO) {
        return R.ok(hazAlarmStatisticsService.rtDataHis(hazRtDataHisDTO));
    }
}
